/*
 * Copyright (C) 2005 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.swing.desktop.worker;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.io.CharArrayWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import nu.zoom.swing.text.ConsoleBean;

/**
 * 
 * @author $Author$
 * @version $Revision$
 * 
 */
public class EventQueuePolicyFrame {
	ConsoleBean console;

	public EventQueuePolicyFrame() {
		super();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueuePolicyFrame app = new EventQueuePolicyFrame();
		app.init();
	}

	void init() {
		console = new ConsoleBean();
		console.setInfoColor(new Color(0, 128, 0));
		JFrame mainFrame = new JFrame("Test harness event queue policy");
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setSize(800, 600);
		mainFrame.setLocation(200, 200);
		mainFrame.getContentPane().setLayout(new BorderLayout());
		mainFrame.getContentPane().add(console, BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel();
		JButton testEventQueueTaskButton = new JButton("testEventQueueTask");
		buttonPanel.add(testEventQueueTaskButton);
		testEventQueueTaskButton.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				try {
					console.println("====");
					testEventQueueTask();
				} catch (Exception e1) {
					CharArrayWriter writer = new CharArrayWriter();
					e1.printStackTrace(new PrintWriter(writer));
					e1.printStackTrace();
					console.error(new String(writer.toCharArray()));
				}
			}
		});
		JButton testThreadTaskButton = new JButton("testThreadTask");
		buttonPanel.add(testThreadTaskButton);
		testThreadTaskButton.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				try {
					console.println("====");
					testThreadTask();
				} catch (Exception e1) {
					CharArrayWriter writer = new CharArrayWriter();
					e1.printStackTrace(new PrintWriter(writer));
					e1.printStackTrace();
					console.error(new String(writer.toCharArray()));
				}
			}
		});
		JButton testBlockingButton = new JButton("testBlocking");
		buttonPanel.add(testBlockingButton);
		testBlockingButton.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				try {
					console.println("====");
					testBlocking();
				} catch (Exception e1) {
					CharArrayWriter writer = new CharArrayWriter();
					e1.printStackTrace(new PrintWriter(writer));
					e1.printStackTrace();
					console.error(new String(writer.toCharArray()));
				}
			}
		});
		mainFrame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
		mainFrame.validate();
		mainFrame.setVisible(true);
	}

	void testEventQueueTask() {
		EventQueueTask task = ObjectFactory.getEventQueuetask(console,
				ObjectFactory.getEventQueueCallback(console), ObjectFactory
						.getThreadCallback(console), new PlainCallback(
						console));
		task.execute();
	}

	void testThreadTask() {
		ThreadTask task = ObjectFactory.getThreadTask(console,
				ObjectFactory.getEventQueueCallback(console), ObjectFactory
						.getThreadCallback(console), new PlainCallback(
						console));
		task.execute();
	}

	void testBlocking() {
		final Blocking blocker = WorkerFactory
				.decorate(new BlockingImpl(console));
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				console.print("Calling thread blocker");
//				console.info("Blocker returned: " + blocker.threadBlock()
//						+ "\n");
//			}
//		});
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				console.print("Calling thread exception");
				try {
					blocker.threadException();
				} catch (Exception e) {
					System.out.println("Blocker threw expected exception" + e);
					console.println("Blocker threw expected exception" + e);
					e.printStackTrace() ;
				}
			}
		});
	}
}
